package com.example.certificate.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.certificate.dto.RoleDto;
import com.example.certificate.dto.common.DelByIdDto;
import com.example.certificate.entity.SysRoleJurisdiction;
import com.example.certificate.entity.Tree;
import com.example.certificate.entity.User;
import com.example.certificate.mapper.SysRoleJurisdictionMapper;
import com.example.certificate.service.SysRoleJurisdictionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.certificate.utils.ResultConstant;
import com.example.certificate.utils.ResultEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hao
 * @since 2022-02-24 15:05:25
 */
@Service
public class SysRoleJurisdictionServiceImpl extends ServiceImpl<SysRoleJurisdictionMapper, SysRoleJurisdiction> implements SysRoleJurisdictionService {

    @Resource
    private SysRoleJurisdictionMapper jurisdictionMapper;

    @Override
    public ResultEntity insertRole(RoleDto dto) {
        jurisdictionMapper.delete(new QueryWrapper<SysRoleJurisdiction>().eq("role_id", dto.getRoleId()));
        JSONArray jsonArray = JSONArray.parseArray(dto.getRoles());
        Iterator<Object> iterator = jsonArray.iterator();
        while (iterator.hasNext()){
            JSONObject jsonObject = (JSONObject) iterator.next();
            SysRoleJurisdiction jurisdiction = new SysRoleJurisdiction();
            jurisdiction.setJurisdictionId(jsonObject.getString("id"));
            jurisdiction.setRoleId(dto.getRoleId());
            jurisdiction.setType(jsonObject.getInteger("nature"));
            jurisdictionMapper.insert(jurisdiction);
        }
        return new ResultEntity();
    }

    @Override
    public List<String> getRoleData(DelByIdDto dto) {
        List<String> list = new ArrayList<>();
        List<SysRoleJurisdiction> role_id = jurisdictionMapper.selectList(new QueryWrapper<SysRoleJurisdiction>().eq("role_id", dto.getId()).eq("type",2));
        role_id.forEach(item ->{
            list.add(item.getJurisdictionId());
        });
        return list;
    }
}
